package org.abacus.platform.grid.impl;

import org.abacus.json.Json;
import org.abacus.platform.core.util.Page;
import org.abacus.platform.grid.Grid;
import org.abacus.platform.grid.GridColumn;
import org.abacus.platform.grid.GridData;

public class GridImpl implements Grid {
	/**
	 *
	 */
	private static final long serialVersionUID = -7268305726402496213L;
	private GridColumn[] gridColumns;
	private GridData[] gridDatas;
	private Page page;
	private boolean check;

	public GridColumn[] getGridColumns() {
		return gridColumns;
	}

	public GridData[] getGridDatas() {
		return gridDatas;
	}

	public void setGridColumns(GridColumn[] arg0) {
		gridColumns = arg0;
	}

	public void setGridDatas(GridData[] arg0) {
		gridDatas = arg0;
	}

	public void setGridColumn(int arg0, GridColumn arg1) {
		gridColumns[arg0] = arg1;
	}

	public void setGridData(int arg0, GridData arg1) {
		gridDatas[arg0] = arg1;
	}

	public GridColumn getGridColumn(int arg0) {
		return gridColumns[arg0];
	}

	public GridData getGridData(int arg0) {
		return gridDatas[arg0];
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page arg0) {
		page = arg0;
	}

	public boolean getCheck() {
		return check;
	}

	public void setCheck(boolean arg0) {
		check = arg0;
	}

	public String toJson() {
		StringBuffer sb = new StringBuffer();
		sb.append('{');

		sb.append('"');
		sb.append("check");
		sb.append('"');
		sb.append(':');
		sb.append(check);
		sb.append(',');

		sb.append('"');
		sb.append("columns");
		sb.append('"');
		sb.append(':');

		sb.append('[');

		GridColumn[] gcs = getGridColumns();
		if (null != gcs) {
			boolean a = true;
			for (int i = 0; i < gcs.length; i++) {
				if (null == gcs[i]) {
					continue;
				}
				gcs[i].toJson(sb);
				if (a) {
					a = false;
				}
				sb.append(',');
			}
			if (!a) {
				sb.deleteCharAt(sb.lastIndexOf(","));
			}
		}

		sb.append(']');

		sb.append(',');

		sb.append('"');
		sb.append("datas");
		sb.append('"');
		sb.append(':');
		sb.append('[');

		GridData[] gds = getGridDatas();
		if (null != gds) {
			boolean a = true;
			for (int i = 0; i < gds.length; i++) {
				if (null == gds[i]) {
					continue;
				}
				gds[i].toJson(sb);
				if (a) {
					a = false;
				}
				sb.append(',');
			}
			if (!a) {
				sb.deleteCharAt(sb.lastIndexOf(","));
			}
		}

		sb.append(']');
		sb.append(',');

		sb.append('"');
		sb.append("page");
		sb.append('"');
		sb.append(':');
		Json j = new Json();
		sb.append(j.toJson(page));
		sb.append('}');
		return sb.toString();
	}

	public String toString() {
		return toJson();
	}
}
